/**
 * 项目添加接口测试用例
 *
 * Created by snowingsea on 2020/02/08.
 */
const chai = require('chai');
const should = chai.should(); // eslint-disable-line no-unused-vars
chai.use(require('chai-http'));
const deepcopy = require('deepcopy');

const common = require('./../../common');
const Administrator = require('./../../../models/administrator');
const Project = require('./../../../models/project');
const uri = require('./../uri');

const projects = require('./../../../../phoenix/models/projects');
const projectVersions = require('./../../../../phoenix/models/project-versions');

module.exports = describe('/POST projects.json', function () {
  const url = '/projects.json';
  const administrator = new Administrator();
  let tester;
  const newUserIds = [];

  before(function (done) {
    administrator.login()
      .then(() => administrator.createRandomTester())
      .then((res) => {
        tester = res;
        newUserIds.push(tester.userId);
        done();
      })
      .catch(done);
  });
  after(function (done) {
    administrator.removeUsers(newUserIds)
      .then(() => {
        done();
      })
      .catch(done);
  });

  common.should.be.user(uri, url, 'post');
  common.should.be.tester(uri, url, 'post');

  // 正常添加项目
  it('it should ADD a project successfully', function (done) {
    const projectInfo = Project.getRandomProjectInfo();
    tester.login()
      .then(() => {
        chai.request(uri)
          .post(url)
          .set('Authorization', `Bearer ${tester.token.accessToken}`)
          .query(projectInfo)
          .end((err, res) => {
            res.should.have.status(200);
            common.should.be.projectMessage(res.body);
            res.body.projectName.should.be.eql(projectInfo.projectName);
            res.body.status.should.be.eql(projects.statusKind.normal);
            res.body.description.should.be.eql(projectInfo.description);
            res.body.lastVersion.versionName.should.be.eql(projectInfo.initVersion);
            res.body.lastVersion.status.should.be.eql(projectVersions.statusKind.normal);
            done();
          });
      })
      .catch(done);
  });

  // 默认初始版本名称为"_"
  it('it should be "_" when initVersion is empty', function (done) {
    const projectInfo = Project.getRandomProjectInfo();
    delete projectInfo.initVersion;
    delete projectInfo.description;
    tester.login()
      .then(() => {
        chai.request(uri)
          .post(url)
          .set('Authorization', `Bearer ${tester.token.accessToken}`)
          .query(projectInfo)
          .end((err, res) => {
            res.should.have.status(200);
            common.should.be.projectMessage(res.body);
            res.body.lastVersion.versionName.should.be.eql('_');
            res.body.description.should.be.eql('');
            done();
          });
      })
      .catch(done);
  });

  // 不同用户可添加相同项目名的项目
  it('it should add a same project to different tester', function (done) {
    const projectInfo = Project.getRandomProjectInfo();
    let newTester;
    tester.createProject(projectInfo)
      .then(() => administrator.createRandomTester())
      .then((tester) => {
        newTester = tester;
        newUserIds.push(tester.userId);
        return newTester.login();
      })
      .then(() => {
        chai.request(uri)
          .post(url)
          .set('Authorization', `Bearer ${newTester.token.accessToken}`)
          .query(projectInfo)
          .end((err, res) => {
            res.should.have.status(200);
            common.should.be.projectMessage(res.body);
            done();
          });
      })
      .catch(done);
  });

  // 重复添加导致失败
  it('it should fail to add project when the project name of the user is exists', function (done) {
    const projectInfo = Project.getRandomProjectInfo();
    tester.createProject(projectInfo)
      .then(() => tester.login())
      .then(() => {
        chai.request(uri)
          .post(url)
          .set('Authorization', `Bearer ${tester.token.accessToken}`)
          .query(projectInfo)
          .end((err, res) => {
            res.should.have.status(409);
            common.should.be.errorMessage(res.body);
            done();
          });
      })
      .catch(done);
  });

  // 项目名称包含特殊字符
  it('it succeeds to add project when the project name contains special characters ', function (done) {
    const projectInfo = Project.getRandomProjectInfo();
    projectInfo.projectName = '≡≠=≤≥<>≮123&_78ǎàóǒ●◎◇◆□■△▲';
    tester.login()
      .then(() => {
        chai.request(uri)
          .post(url)
          .set('Authorization', `Bearer ${tester.token.accessToken}`)
          .query(projectInfo)
          .end((err, res) => {
            res.should.have.status(200);
            common.should.be.projectMessage(res.body);
            res.body.projectName.should.be.eql(projectInfo.projectName);
            done();
          });
      })
      .catch(done);
  });

  // 缺少参数
  const query = {
    projectName: Project.getRandomProjectInfo().projectName,
  };
  Object.keys(query).forEach((key) => {
    it(`it should deny invalid request of missing "${key}"`, function (done) {
      const invalidQuery = deepcopy(query);
      delete invalidQuery[key];
      tester.login()
        .then(() => {
          chai.request(uri)
            .post(url)
            .set('Authorization', `Bearer ${tester.token.accessToken}`)
            .query(invalidQuery)
            .end((err, res) => {
              res.should.have.status(400);
              common.should.be.errorMessage(res.body);
              done();
            });
        })
        .catch(done);
    });
  });
});
